﻿namespace DepartmentSpecificStoreView
{
    using System;
    using System.Collections.Generic;

    using Microsoft.SharePoint;

    public class SecurityGroupBL
    {
        #region Fields

        private List<DepartmentSecurityGroup> _DepartmentSecurityGroups;

        #endregion Fields

        #region Properties

        public List<DepartmentSecurityGroup> DepartmentSecurityGroups
        {
            get
            {
                if ( _DepartmentSecurityGroups == null )
                    _DepartmentSecurityGroups = new List<DepartmentSecurityGroup>( );
                return _DepartmentSecurityGroups;
            }
        }

        #endregion Properties

        #region Methods

        public void LoadSelectedSecurityGroups( SPWeb storeWeb, string departmentName )
        {
            try
            {

                DepartmentSecurityGroup singleGroup = new DepartmentSecurityGroup( );

                SPList departmentListValues = storeWeb.Lists[ "Department Store Assignment" ];
                SPQuery selectedSecurityGroup = new SPQuery( );

                selectedSecurityGroup.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + departmentName + "</Value></Eq></Where>";
                selectedSecurityGroup.ViewFields = "<FieldRef Name='Store_x0020_Security_x0020_Group'/><FieldRef Name='Department_x0020_Name'/>";
                SPListItemCollection selectedGroups = departmentListValues.GetItems( selectedSecurityGroup );
                foreach ( SPListItem item in selectedGroups )
                {
                    singleGroup = new DepartmentSecurityGroup( );

                    if ( item[ "Store Security Group" ] != null )
                        singleGroup.SecurityGroup = item[ "Store Security Group" ].ToString( );
                    if ( item[ "Department Name" ] != null )
                        singleGroup.DepartmentName = item[ "Department Name" ].ToString( );

                    DepartmentSecurityGroups.Add( singleGroup );

                }
            }

            catch ( Exception )
            {

            }
        }

        #endregion Methods
    }
}